package com.njupt.swg.service;

import com.njupt.swg.mapper.TbItemCatMapper;
import com.njupt.swg.pojo.EasyUITreeNode;
import com.njupt.swg.pojo.TbItemCat;
import com.njupt.swg.pojo.TbItemCatExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<EasyUITreeNode> getItemCatList(long pid) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        List<TbItemCat> list = itemCatMapper.selectByExample(example);
        List<EasyUITreeNode> result = new ArrayList<>();
        for(TbItemCat cat:list){
            EasyUITreeNode treeNode = new EasyUITreeNode();
            treeNode.setId(cat.getId());
            treeNode.setText(cat.getName());
            treeNode.setState(cat.getIsParent()?"closed":"open");
            result.add(treeNode);
        }
        return result;
    }
}
